<?php
/**
 * @Author: rocky
 * Date: 2019/12/11
 * Time: 14:17
 */


namespace app\shop\controller\api\v2;


use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\model\User;
use app\shop\model\ShopSeckillActivity;
use app\shop\model\ShopSpellGood;
use app\shop\resource\SeckillActivityResoure;
use app\shop\resource\SeckillGoodResoure;
use app\shop\resource\SpellGoodResoure;

class Activity extends BaseApiController
{
    //拼团商品列表
    public function getSpellGood(){
        $data = ShopSpellGood::with(['good'=>function($q){
            $q->where('is_deleted',0);
        }])->pages()->select();
        $this->successCode(SpellGoodResoure::collection($data));
    }
    //秒杀商品列表
    public function getSeckillGood(){
        $type = $this->request->get('type');
        if($type == 1){
            $data =ShopSeckillActivity::whereBetweenTimeField('start_time','end_time')->with(['good'=>function($q){
                $q->where('is_deleted',0)->removeOption('order')->pages()->order('pivot.sort asc');
            }])->find();
            $data['activity_time'] = $data['end_time'];
        }else{
            $data =ShopSeckillActivity::whereTime('start_time','>',time())->with(['good'=>function($q){
                $q->where('is_deleted',0)->removeOption('order')->pages()->order('pivot.sort asc');
            }])->removeOption('order')->order('start_time asc')->find();
            $data['activity_time'] = $data['start_time'];
        }
        $this->successCode(SeckillActivityResoure::item($data));
    }
}